package yur.wedding.modules.sys.controller;


import java.util.Arrays;
import java.util.List;
import java.util.Map;



import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import yur.wedding.common.annotation.SysLog;
import yur.wedding.modules.sys.entity.SysRoleEntity;
import yur.wedding.modules.sys.service.*;

import yur.wedding.utils.PageUtils;
import yur.wedding.utils.R;
import yur.wedding.validator.ValidatorUtils;


/**
 * 角色
 *
 * @author Yur
 * @email ${email}
 * @date 2021-11-09 21:15:10
 */
@RestController
@RequestMapping("sys/sysrole")
public class SysRoleController {
    @Autowired
    private SysRoleService sysRoleService;


    @Autowired
    private SysRoleDeptService roleDeptService;

    @Autowired
    private SysRoleMenuService roleMenuService;

    @Autowired
    private SysUserRoleService sysUserRoleService;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private  SysRoleDoorService sysRoleDoorService;
    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:sysrole:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = sysRoleService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 角色列表
     */
    @RequestMapping("/select")
    @RequiresPermissions("sys:sysrole:select")
    public R select() {

        List<SysRoleEntity> list = sysRoleService.list();

        return R.ok().put("roleList", list);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{role_id}")
    @RequiresPermissions("sys:sysrole:info")
    public R info(@PathVariable("role_id") Long role_id) {
        SysRoleEntity sysRole = sysRoleService.getById(role_id);

        //查询角色对应的菜单权限
        List<Long> menuIdlist = roleMenuService.queryMenuIdList(role_id);
        sysRole.setMenuIdList(menuIdlist);
        //查询角色对应的 部门权限   之前查询写过直接用
        List<Long> deptIdList = roleDeptService.queryDeptIdList(new Long[]{role_id});
        sysRole.setDeptIdList(deptIdList);

        //查询角色对应的 门户权限
        List<Long> doorIdlist =sysRoleDoorService.queryDoorIdList(role_id);
        sysRole.setDoorIdList(doorIdlist);
        System.out.println("查询单个角色" + sysRole);
        return R.ok().put("role", sysRole);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:sysrole:save")
    public R save(@RequestBody SysRoleEntity sysRole) {

        System.out.println("保存角色，同时保存菜单权限，与数据权限 ，与门户权限:" + sysRole);
        sysRoleService.saveRole(sysRole);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:sysrole:update")
    public R update(@RequestBody SysRoleEntity sysRole) {
        System.out.println("xg：---" + sysRole);
        ValidatorUtils.validateEntity(sysRole);
        sysRoleService.updateRole(sysRole);

        return R.ok();
    }

    /**
     * 删除
     */
    @SysLog("删除角色")
    @RequestMapping("/delete")
    @RequiresPermissions("sys:sysrole:delete")
    public R delete(Long roleIds) {
        sysRoleService.removeByIds(Arrays.asList(roleIds));

        return R.ok();
    }

    /**
     * 获取角色下面有那些用户
     */
    @RequestMapping("/roleuserlist/{role_id}")
    @RequiresPermissions("sys:sysrole:roleuserlist")
    public R roleuserlist(@PathVariable("role_id") Long role_id,@RequestParam Map<String, Object> params) {

        PageUtils page = sysRoleService.roleuserlist(role_id,params);
        return R.ok().put("page", page);
    }


    /**
     * 删除角色下面的用户  可批量删除
     */
    @RequestMapping("/roleuserdelete")
    @RequiresPermissions("sys:sysrole:roleuserdelete")
    public R roleuserdelete(@RequestBody String userIdroleIds) {

        JSONObject jsonObject = JSONObject.fromObject(userIdroleIds);
        JSONArray jsonArr = (JSONArray) jsonObject.get("userIDs");
        String roleId1 = (String) jsonObject.get("roleid");
        Long roleId=Long.valueOf(roleId1);

        Long useridList[] = new Long[jsonArr.size()];
        for (int i = 0; i < jsonArr.size(); i++) {
            useridList[i] = Long.valueOf(jsonArr.get(i).toString());
        }
         sysUserRoleService.removeUserIdList(useridList,roleId);
        return R.ok();
    }



    /**
     * 角色添加用户时 查找
     */
    @RequestMapping("/roleuserselectlist")
    @RequiresPermissions("sys:sysrole:roleuserselectlist")
    public R roleuserselectlist(@RequestParam Map<String, Object> params) {
        System.out.println("模糊条件:"+StringUtils.isNotBlank((String) params.get("lastname_s"))+"-----"+StringUtils.isNotBlank((String) params.get("workcoke_s")));

        if (StringUtils.isNotBlank((String) params.get("lastname_s")) || StringUtils.isNotBlank((String) params.get("workcoke_s")) ){
                System.out.println("执行角色中的用户模糊查询");
            PageUtils page =sysUserService.queryRoleListPage(params);
            return R.ok().put("page", page);
        }

        return R.ok().put("page", new PageUtils(null,0,0,0));
    }


    /**
     * 删除角色下面的用户  可批量删除
     */
    @RequestMapping("/roleusersave")
    @RequiresPermissions("sys:sysrole:roleuserdelete")
    public R roleusersave(@RequestBody String userIdroleIds) {

        sysUserRoleService.removeUserIdsave(userIdroleIds);
        return R.ok();
    }



}
